/*
 ============================================================================
 Name		: FEAppView.cpp
 Author	  : Gao QianXuan
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CFEAppDirView implementation
 ============================================================================
 */

#include <aknviewappui.h>
#include <avkon.hrh>
#include "FE.hrh"
#include <FE_0xE986B88E.rsg>
#include "FEAppDirView.h"
#include "FEAppDirContainer.h"

CFEAppDirView::CFEAppDirView()
{
	// No implementation required
}

CFEAppDirView::~CFEAppDirView()
{
	if (iContainer != NULL)
	{
		AppUi()->RemoveFromViewStack(*this, iContainer);
		delete iContainer;
		iContainer = NULL;
	}
}

CFEAppDirView* CFEAppDirView::NewLC()
{
	CFEAppDirView* self = new (ELeave) CFEAppDirView();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CFEAppDirView* CFEAppDirView::NewL()
{
	CFEAppDirView* self = CFEAppDirView::NewLC();
	CleanupStack::Pop(); // self;
	return self;
}

void CFEAppDirView::ConstructL()
{
	BaseConstructL(R_DIR_VIEW);
}

/******************************************************************************
 * @Name:       Id 
 * @Author:     Qu JunJie
 * @Purpose:    Returns the Uid of Directory view
 * @Returns:    Uid EDirView
 * @Parameters: None
 * @Remarks:    -
 ******************************************************************************/

TUid CFEAppDirView::Id() const
{
	return TUid::Uid(EDirView);
}

/******************************************************************************
 * @Name:       HandleCommandL 
 * @Author:     Qu JunJie / Gao QianXuan
 * @Purpose:    Handle commands user entered from Options menu
 * @Returns:    void
 * @Parameters: None
 * @Remarks:    -
 ******************************************************************************/

void CFEAppDirView::HandleCommandL(TInt aCommand)
{
	switch (aCommand)
	{
		case EOpen:
		{
			TInt idx = iContainer->GetListBoxCtrl()->CurrentItemIndex();
			iContainer->OpenL(idx);
			break;
		}

		case ENewFolder:
		{
			iContainer->NewFolderL();
			break;
		}

		case EAknSoftkeyBack:
		case EBack:
		{
			iContainer->GoBackToUpperDirL();
			break;
		}

		case EBackToTop:
		{
			AppUi()->ActivateLocalViewL(TUid::Uid(EDriverView));
			break;
		}

		case EDelete:
		{
			TInt idx = iContainer->GetListBoxCtrl()->CurrentItemIndex();
			iContainer->DeleteL(idx);
			break;
		}

		case ERename:
		{
			TInt idx = iContainer->GetListBoxCtrl()->CurrentItemIndex();
			iContainer->RenameL(idx);
			break;
		}

		case ECopy:
		{
			TInt idx = iContainer->GetListBoxCtrl()->CurrentItemIndex();
			iContainer->Copy(idx);
			break;
		}

		case EMove:
		{
			TInt idx = iContainer->GetListBoxCtrl()->CurrentItemIndex();
			iContainer->Move(idx);
			break;
		}

		case EPaste:
		{
			iContainer->PasteL();
			break;
		}

		case EProperties:
		{
			TInt idx = iContainer->GetListBoxCtrl()->CurrentItemIndex();
			iContainer->GetPropertiesL(idx);
			break;
		}

		case EEikCmdExit:
		case EAknSoftkeyExit:
		{
			AppUi()->Exit();
			break;
		}

		default:
			break;
	}
}

/*******************************************************************************
 * @Name:       HandleClientRectChange 
 * @Author:     Junjie Qu
 * @Purpose:    Handle screen resolution change.
 * @Returns:    void
 * @Parameters: void
 * @Remarks:    -
 ******************************************************************************/
void CFEAppDirView::HandleClientRectChange()
{
	if (iContainer != NULL)
	{
		iContainer->SetRect(ClientRect());
	}
}

/******************************************************************************
 * @Name:       DoActivateL 
 * @Author:     Qu JunJie
 * @Purpose:    Activate the Directory container window
 * @Returns:    void
 * @Parameters: const TVwsViewId&, 
 * 				TUid,
 *				const TDesC8 &aFileName
 * @Remarks:    -
 ******************************************************************************/

void CFEAppDirView::DoActivateL(const TVwsViewId&, TUid,
		const TDesC8 &aFileName)
{
	if (iContainer == NULL)
	{
		iContainer = new (ELeave) CFEAppDirContainer;
		CleanupStack::PushL(iContainer);

		iFileName.Copy(aFileName);
		iContainer->SetPath(iFileName);

		iContainer->ConstructL(ClientRect());
		CleanupStack::Pop(iContainer);

		iContainer->SetMopParent(this);
		AppUi()->AddToStackL(*this, iContainer);

		iContainer->SetView(this);
	}
}

/******************************************************************************
 * @Name:       DoDeactivate
 * @Author:     Qu JunJie
 * @Purpose:    Deactivate the Directory container window
 * @Returns:    void
 * @Parameters: None
 * @Remarks:    -
 ******************************************************************************/

void CFEAppDirView::DoDeactivate()
{
	if (iContainer != NULL)
	{
		AppUi()->RemoveFromViewStack(*this, iContainer);
		delete iContainer;
		iContainer = NULL;
	}
}

/******************************************************************************
 * @Name:       SetPath
 * @Author:     Qu JunJie
 * @Purpose:    Set the directory path to user defined path
 * @Returns:    void
 * @Parameters: TFileName &aFileName
 * @Remarks:    -
 ******************************************************************************/

void CFEAppDirView::SetPath(TFileName &aFileName)
{
	iFileName = aFileName;
}

/******************************************************************************
 * @Name:       GetAppUi
 * @Author:     Qu JunJie
 * @Purpose:    Returns the AppUi
 * @Returns:    CAknViewAppUi AppUi();
 * @Parameters: None
 * @Remarks:    -
 ******************************************************************************/

CAknViewAppUi *CFEAppDirView::GetAppUi()
{
	return AppUi();
}
